﻿using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Cookie.Library.Properties;

namespace Cookie.Library.Controls
{
    /*2012年2月2日，因去除Gean引用，本类型不再使用*/
    public sealed class ExListBox : ListBox//, IOptionItemManager
    {
        private readonly Image _DefaultConfigImage;

        /// <summary>
        /// 创建 ExListBox class 的新实例
        /// </summary>
        public ExListBox()
        {
            MeasureItem += ExListBoxMeasureItem;
            DrawItem += ExListBoxDrawItem;
            DrawMode = DrawMode.OwnerDrawVariable;
            _DefaultConfigImage = Resources.ComputeFlag;
        }

        #region IOptionItemManager Members

        //public IEnumerable<object> Initialize(IEnumerable<object> optionItems)
        //{
        //    var controls = new List<object>();
        //    foreach (object optionItem in optionItems)
        //    {
        //        Items.Add(optionItem);
        //        //controls.Add(optionItem.OptionUI);
        //    }
        //    return controls;
        //}

        public void Initialize(IEnumerable<object> optionItems)
        {
            foreach (object optionItem in optionItems)
            {
                Items.Add(optionItem);
                //controls.Add(optionItem.OptionUI);
            }
        }

        #endregion

        private void ExListBoxDrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0 || e.Index >= Items.Count)
                return;

            var cfgItem = Items[e.Index] as object;
            if (cfgItem == null) return;

            e.DrawBackground();
            e.DrawFocusRectangle();

            Graphics g = e.Graphics;

            g.DrawImage(_DefaultConfigImage,
                        new Rectangle(new Point(3 + e.Bounds.Left, 3 + e.Bounds.Top), new Size(16, 16)));

            var brush = new SolidBrush(e.ForeColor);
//            g.DrawString(cfgItem.MenuName, new Font("宋体", 12.0F, FontStyle.Regular, GraphicsUnit.Pixel), brush,
//                         new PointF(23 + e.Bounds.Left, 5 + e.Bounds.Top));
        }

        private void ExListBoxMeasureItem(object sender, MeasureItemEventArgs e)
        {
            if (e.Index >= Items.Count) return;

//            var cfgItem = Items[e.Index] as IOptionListItem;
//            if (cfgItem == null)
//                return;
//
//            e.ItemHeight = cfgItem.MenuHeight;
        }
    }
}